package com.resou.reader.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.bytedance.sdk.openadsdk.TTNativeAd;
import com.google.gson.Gson;
import com.resou.reader.ReSouApplication;
import com.resou.reader.api.entry.LoginData;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.reader.utils.ScreenUtils;

public class PreferenceUtil {
    private static final String TO_BE_DELETED = "To_be_deleted";
    private static final String SEX = "sex";
    private static final String SHOW_RECHARGE_DIALOG = "show_recharge_dialog";
    private static final String SHOW_FIFTEEN_DAYS_DIALOG = "show_fifteen_days_dialog";
    private static final String SHOW_COLLECTION_DIALOG = "show_collection_dialog";
    private static final String USER_INFO = "user_info";
    public static final String KEY_IS_NIGHT_MODE = "is_night_mode";
    public static final String KEY_IS_SYSTEM_BRIGHTNESS = "is_system_brightness";
    public static final String KEY_BRIGHTNESS = "brightness";
    public static final String KEY_BRIGHTNESS_IN_NIGHT_MODE = "brightness_in_night_mode";
    public static final String KEY_IS_SYSTEM_BRIGHTNESS_IN_NIGHT_MODE = "is_system_brightness_in_night_mode";

    private static final String SUBSIDYSTATE = "SUBSIDYSTATE";
    private static final String LOGINSTATE = "loginstate";
    public static final String TOKEN = "token";
    private static PreferenceUtil sInstance;
    private final SharedPreferences mPreferences;

    public static PreferenceUtil getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new PreferenceUtil(context);
        }
        return sInstance;
    }

    private PreferenceUtil(Context context) {
        mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    }

    public void saveFreeBooks(String userId, String bookId) {
        mPreferences.edit().putBoolean(userId + bookId, true).apply();
    }

    public boolean checkIfBookFree(String userId, String bookId) {
        return mPreferences.getBoolean(userId + bookId, false);
    }

    public String loadUserToken() {
        return mPreferences.getString(TOKEN, "");
    }

    public String loadUserUserType() {
        return mPreferences.getString(TOKEN, "");
    }

    public void saveUserToken(String token) {
        mPreferences.edit().putString(TOKEN, token).apply();
    }

    public LoginData loadLoginData() {
        String string = mPreferences.getString(USER_INFO, "");
        return ReSouApplication.gson.fromJson(string, LoginData.class);
    }

    public void saveLoginData(LoginData data) {
        String loginJson = ReSouApplication.gson.toJson(data);
        mPreferences.edit().putString(USER_INFO, loginJson).apply();
    }

    /**
     * 获取用户性别
     *
     * @return 男生：1，女生：2,跳过：3
     */
    public int loadUserGender() {
        return mPreferences.getInt(SEX, 0);
    }

    /**
     * 保存用户性别
     *
     * @param gender 男生：1，女生：2,跳过：3
     */
    public void saveUserGender(int gender) {
        mPreferences.edit().putInt(SEX, gender).apply();
    }

    public boolean isShowRechargeDialogShowInReader() {
        return mPreferences.getBoolean(SHOW_RECHARGE_DIALOG, true);
    }

    /**
     * 设置
     */
    public void setNotShowRechargeDialogToday() {
        mPreferences.edit().putBoolean(SHOW_RECHARGE_DIALOG, false).apply();
    }

    /**
     * 设置显示充值对话框
     */
    public void setShowRechargeDialogToday(boolean isFirst) {
        mPreferences.edit().putBoolean(SHOW_RECHARGE_DIALOG, isFirst).apply();
    }

    /**
     * 判断是否显示十五天活动对话框
     *
     * @return
     */
    public boolean isShowFifteenDaysDialog() {
        return mPreferences.getBoolean(SHOW_FIFTEEN_DAYS_DIALOG, false);
    }

    public void saveAdData(TTNativeAd data) {
        String ad = ReSouApplication.gson.toJson(data);
        mPreferences.edit().putString("Ad", ad).apply();
    }

    /**
     * 判断是否显示收藏的弹窗
     *
     * @return
     */
    public boolean isShowCollectionDialog() {
        return mPreferences.getBoolean(SHOW_COLLECTION_DIALOG, true);
    }

    /**
     * j将显示的弹窗 设置false下次不弹出
     */
    public void setShowCollectionDialog() {
        mPreferences.edit().putBoolean(SHOW_COLLECTION_DIALOG, false).apply();
    }

    /**
     * 设置为不显示十五天活动对话框
     * 目前隐藏15天活动所以设置为true
     */
    public void setNeverShowFifteenDaysDialogState() {
        mPreferences.edit().putBoolean(SHOW_FIFTEEN_DAYS_DIALOG, true).apply();
    }

    /**
     * 设置当天显示十五天活动对话框
     */
    public void setShowFifteenDaysDialog() {
        mPreferences.edit().putBoolean(SHOW_FIFTEEN_DAYS_DIALOG, true).apply();
    }

    /**
     * 设置是否打开阅读引导页
     */
    public void setReadGuide(boolean isOPen) {
        mPreferences.edit().putBoolean("isOPenGuide", isOPen).apply();
    }

    /**
     * 获取是否打开阅读引导页
     */
    public boolean getReadGuide() {
        return mPreferences.getBoolean("isOPenGuide", true);
    }

    /**
     * 保存音量键是否翻页状态
     *
     * @param volume
     */
    public void saveVolume(boolean volume) {
        mPreferences.edit().putBoolean(UserInstance.getToken() + "", volume).apply();
    }

    /**
     * 保存初始加载弹框显示
     *
     * @param subSidy
     */
    public void saveSubSidyState(boolean subSidy) {
        mPreferences.edit().putBoolean("subSidy", subSidy).apply();
    }

    /**
     * 获取初始加载弹框的状态
     */
    public boolean getSubSidyState() {
        return mPreferences.getBoolean("subSidy", true);
    }

    /**
     * 获取音量键是否翻页状态
     */
    public boolean getVolumeState() {
        return mPreferences.getBoolean(UserInstance.getToken() + "", false);
    }

    /**
     * 将本地
     *
     * @param comment
     */
    public void setlocalCommentPariseState(String comment) {
        mPreferences.edit().putBoolean(comment + UserInstance.getUserId(), true).apply();
    }

    /**
     * 获获取本地是否已经被点赞过
     */
    public boolean getlocalCommentPariseState(String comment) {
        return mPreferences.getBoolean(comment + UserInstance.getUserId(), false);
    }

    /**
     * 分享获取免费阅读时长
     */
    public void setShareTime(long time) {
        mPreferences.edit().putLong("shareTime" + UserInstance.getUserId(), time).apply();
    }

    /**
     * 保存分享状态
     */
    public void setIsShare(boolean isShare) {
        mPreferences.edit().putBoolean("isShare" + UserInstance.getUserId(), isShare).apply();
    }

    /**
     * 是否已经分享过
     */
    public boolean getIsShare() {
        return mPreferences.getBoolean("isShare" + UserInstance.getUserId(), false);
    }

    /**
     * 保存节省金额
     */
    public void setMoney(float money) {
        mPreferences.edit().putFloat("m" + UserInstance.getUserId(), money).apply();
    }

    /**
     * 获取节省金额
     */
    public float getMoney() {
        return mPreferences.getFloat("m" + UserInstance.getUserId(), 0);
    }

    /**
     * 获取免费阅读时长
     */
    public long getShareTime() {
        return mPreferences.getLong("shareTime" + UserInstance.getUserId(), 0);
    }

    /**
     * 分享时间
     */
    public void setShareDate(String date) {
        mPreferences.edit().putString("shareDate", date).apply();
    }

    /**
     * 获取分享时间
     */
    public String getShareDate() {
        return mPreferences.getString("shareDate", "");
    }


//    /**
//     * 获取记录广告的标记
//     */
//    public int getAdValue(String key) {
//        return mPreferences.getInt(key, 1001);
//    }
//
//    /**
//     * 记录广告的标记
//     *
//     * @param value
//     */
//    public void saveAdValue(String key, int value) {
//        mPreferences.edit().putInt(key, value).apply();
//    }

    /**
     * 保存用户的补贴模式的状态
     *
     * @param value
     */
    public void saveSubSidyStateValue(int value) {
        mPreferences.edit().putInt(SUBSIDYSTATE + UserInstance.getUserId(), value).apply();
    }

    /**
     * 获取用户补贴模式的状态
     */
    public int getSubSidyStateValue() {
        return mPreferences.getInt(SUBSIDYSTATE + UserInstance.getUserId(), 0);
    }

    /**
     * 获取是否需要更新app版本状态
     *
     * @return
     */
    public boolean getIsUpdateApp() {
        return mPreferences.getBoolean("isUpdateApp", true);
    }

    public void setIsUpdateApp(boolean str) {
        mPreferences.edit().putBoolean("isUpdateApp", str).apply();
    }

    /**
     * 保存书架展示方式 图墙or列表
     */
    public boolean getIsListShow() {
        return mPreferences.getBoolean("isListShow", true);
    }

    public void setIsListShow(boolean value) {
        mPreferences.edit().putBoolean("isListShow", value).apply();
    }

    /**
     * 用户头像存储地址
     */
    public String getUserIcon() {
        return mPreferences.getString("usericon", null);
    }

    public void setUserIcon(String path) {
        mPreferences.edit().putString("usericon", path).apply();
    }

    /**
     * 用户登录状态
     */
    public void saveLoginState(int state) {
        mPreferences.edit().putInt(LOGINSTATE, state).apply();
        Log.i("LOGINSTATE", "saveLoginState: " + mPreferences.getInt(LOGINSTATE, 0));
        Log.i("LOGINSTATE", "saveLoginState2: " + state);
    }

    /**
     * 获取用户登录状态
     * 0手机号  1QQ  2微信
     */
    public int getLoginState() {
        return mPreferences.getInt(LOGINSTATE, 0);
    }

    /**
     * 保存开屏广告概率
     */
    public void saveAdProbability(int value) {
        mPreferences.edit().putInt("KPAd", value).apply();
    }

    /**
     * 获取开屏广告概率
     *
     * @return
     */
    public int getAdProbability() {
        return mPreferences.getInt("KPAd", 50);
    }


    /**
     * 保存阅读器广告概率
     */
    public void saveAdProbabilityContent(int value) {
        mPreferences.edit().putInt("NRAd", value).apply();
    }

    /**
     * 获取阅读器广告概率
     *
     * @return
     */
    public int getAdProbabilityContent() {
        return mPreferences.getInt("NRAd", 50);
    }


    public int getBrightness(Context context) {
        if (!getIsNightMode()) {
            return getBrightnessInDayMode(context);
        }
        return getBrightnessInNightMode();
    }

    public int getBrightnessInNightMode() {
        return mPreferences.getInt(KEY_BRIGHTNESS_IN_NIGHT_MODE, 1);
    }

    public int getBrightnessInDayMode(Context context) {
        int value = mPreferences.getInt(KEY_BRIGHTNESS, -1);
        if (-1 == value) {
            value = ScreenUtils.getSystemBrightness(context);
        }
        return value;
    }

    public boolean getIsSystemBirghtness() {
        if (!getIsNightMode()) {
            return getIsSystemBirghtnessInDayMode();
        }
        return getIsSystemBirghtnessInNightMode();
    }

    /**
     * @param number 1~100
     */
    public void setBrightness(int number) {
        if (!getIsNightMode()) {
            setBrightnessInDayMode(number);
        } else {
            setBrightnessInNightMode(number);
        }
    }

    public void setIsSystemBirghtness(boolean flag) {
        if (!getIsNightMode()) {
            setIsSystemBirghtnessInDayMode(flag);
        } else {
            setIsSystemBirghtnessInNightMode(flag);
        }
    }

    public void setIsSystemBirghtnessInNightMode(boolean flag) {
        mPreferences.edit().putInt(KEY_IS_SYSTEM_BRIGHTNESS_IN_NIGHT_MODE, flag ? 1 : 0).commit();
    }


    public void setIsSystemBirghtnessInDayMode(boolean flag) {
        mPreferences.edit().putInt(KEY_IS_SYSTEM_BRIGHTNESS, flag ? 1 : 0).apply();
    }

    /**
     * @param number 1~100
     */
    public void setBrightnessInDayMode(int number) {
        mPreferences.edit().putInt(KEY_BRIGHTNESS, number).apply();
    }

    public void setIsNightMode(boolean flag) {
        mPreferences.edit().putInt(KEY_IS_NIGHT_MODE, flag ? 1 : 0).apply();
    }

    /**
     * @param number 1~100
     */
    public void setBrightnessInNightMode(int number) {
        mPreferences.edit().putInt(KEY_BRIGHTNESS_IN_NIGHT_MODE, number).apply();
    }


    public boolean getIsNightMode() {
        int flag = mPreferences.getInt(KEY_IS_NIGHT_MODE, 0);
        return (flag == 1);
    }


    public boolean getIsSystemBirghtnessInDayMode() {
        int flag = mPreferences.getInt(KEY_IS_SYSTEM_BRIGHTNESS, 1);
        return (flag == 1);
    }

    public boolean getIsSystemBirghtnessInNightMode() {
        int flag = mPreferences.getInt(KEY_IS_SYSTEM_BRIGHTNESS_IN_NIGHT_MODE, 1);
        return (flag == 1);
    }
}
